﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.Table;

namespace chatblog.bbsmanage
{
    public partial class newsiteapp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                dealrequest();
                //bind1();
                //bind2();
                //bind3();
            }
        }

        private void dealrequest()
        {
            if (AliApi.appUserId == "")
            {
                //Response.Redirect("login.aspx?redirecturl=" + ResolveUrl("~/bbsmanage/newsiteapp.aspx"));
                Button1.Enabled = false;
                Button1.Text = "提交(请先登录)";
            }
            else
            {
                Button1.Enabled = true;
                Button1.Text = "提交";
            }
            if (AliApi.appUserId != "")
            {
                cbuser us = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "' ");
                HyperLink_link2.Text = "http://"+us.RECID.ToString()+".bbs.f980.com/";
                HyperLink_link2.NavigateUrl = "http://" + us.RECID.ToString() + ".bbs.f980.com/";
                cbarea u = cbarea.Find(" cbuserid = '" + AliApi.appUserId + "' ");
                if (u.Count > 0)
                {
                    Button1.Enabled = false;
                    Button1.Text = "提交(已创建网站)";
                }
            }
        }



        protected void Button1_Click(object sender, EventArgs e)
        {
            cbarea area;
            if (TextBox2.Text.Trim().Length > 0)
            {
                area = cbarea.Find(" domainname1 = '" + TextBox2.Text.Trim() + "'"
                    + " or domainname2 = '" + TextBox2.Text.Trim() + "'"
                    + " or domainname3 = '" + TextBox2.Text.Trim() + "'"
                    + " or domainname4 = '" + TextBox2.Text.Trim() + "'"
                    );
                if (area.Count>0)
                {
                    MessageBox.Show(this.Page,"域名已经存在.提交失败.");
                    return;
                }
            }
            if (TextBox3.Text.Trim().Length > 0)
            {
                area = cbarea.Find(" domainname1 = '" + TextBox3.Text.Trim() + "'"
                    + " or domainname2 = '" + TextBox3.Text.Trim() + "'"
                    + " or domainname3 = '" + TextBox3.Text.Trim() + "'"
                    + " or domainname4 = '" + TextBox3.Text.Trim() + "'"
                    );
                if (area.Count > 0)
                {
                    MessageBox.Show(this.Page, "域名已经存在.提交失败.");
                    return;
                }
            }
            if (TextBox4.Text.Trim().Length > 0)
            {
                area = cbarea.Find(" domainname1 = '" + TextBox4.Text.Trim() + "'"
                    + " or domainname2 = '" + TextBox4.Text.Trim() + "'"
                    + " or domainname3 = '" + TextBox4.Text.Trim() + "'"
                    + " or domainname4 = '" + TextBox4.Text.Trim() + "'"
                    );
                if (area.Count > 0)
                {
                    MessageBox.Show(this.Page, "域名已经存在.提交失败.");
                    return;
                }
            }
            if (TextBox5.Text.Trim().Length > 0)
            {
                area = cbarea.Find(" domainname1 = '" + TextBox5.Text.Trim() + "'"
                    + " or domainname2 = '" + TextBox5.Text.Trim() + "'"
                    + " or domainname3 = '" + TextBox5.Text.Trim() + "'"
                    + " or domainname4 = '" + TextBox5.Text.Trim() + "'"
                    );
                if (area.Count > 0)
                {
                    MessageBox.Show(this.Page, "域名已经存在.提交失败.");
                    return;
                }
            }
            cbuser user = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "' ");
            user.cbname.Value = TextBox7.Text;
            user.cbsex.Value = DropDownList1.Text;
            user.jctel.Value = TextBox9.Text;
            user.jcmobile.Value = TextBox8.Text;
            user.cbqq.Value = TextBox10.Text;
            user.cbmsn.Value = TextBox11.Text;
            user.jcdesc.Value = TextBox12.Text;
            user.Modify();

            cbarea u = cbarea.Find(" cbuserid = '" + AliApi.appUserId + "' ");
            u.cbareaid.Value = u.GetRecID().ToString();
            u.cbcataid.Value = u.cbareaid.ToString();
            u.cbname.Value = TextBox1.Text;
            u.cbtitle.Value = TextBox1.Text;
            u.domainname1.Value = TextBox2.Text;
            u.domainname2.Value = TextBox3.Text;
            u.domainname3.Value = TextBox4.Text;
            u.domainname4.Value = TextBox5.Text;
            u.jcdesc.Value = TextBox6.Text;
            u.enableareabtn.Value = RuleCode.Enum.NoYes.Yes;
            u.enableask.Value = RuleCode.Enum.NoYes.Yes;
            u.cbuserid.Value = AliApi.appUserId;
            u.Insert();
            //增加板块
            cbcata c = cbcata.Find(" 1=2 ");
            c.cbcataid.Value = u.cbcataid.ToString();
            c.pid1.Value = "1";
            c.cbname.Value = "『"+u.cbname.ToString()+"』";
            c.createuserid.Value = AliApi.appUserId;
            c.jccreatedate.Value = DateTime.Now;
            c.cbstatus.Value = u.cbcataid.ToString();
            c.cblayer.Value = 0;
            c.Insert();
            cbcata.createdefaultcata(AliApi.appUserId, c.cbcataid.ToString(),u.cbareaid.ToString());
            //增加自己为版主
            cbbanzhu bz = cbbanzhu.Find(" 1=2 ");
            bz.cbcataid.Value = u.cbcataid.ToString();
            bz.cbuserid.Value = AliApi.appUserId;
            bz.Insert();
            Response.Redirect(ResolveUrl("~/bbsmanage/cata.aspx"));
        }
    }
}
